// 广告路由
import adv from '@/views/adv.ejs';
import axios from '@/api/index.js';


async function getadvs(res,p) {
    let { data:{ advs:advdata,pageCount } } = await axios.get("/adv/get",{
        params:{
            p
        }
    });
    res.render(adv({
        p,
        advdata,
        pageCount,
        baseURL:axios.defaults.baseURL
    }));
    addEvent(res);

}
let p = 1;
function addEvent(res){

    let mask = document.querySelector(".mask");
        let adduermodel = document.querySelector(".adduermodel");
        let titl = document.querySelector("h3"); 
        document.querySelector(".addadv").onclick = function(){
            titl.innerHTML = "添加广告";
            mask.style.display = "block";
            adduermodel.style.display = "block"; 
        }
        document.querySelector(".closebtn").onclick = function(){
            mask.style.display = "none";
            adduermodel.style.display = "none";
        }

        // 选择图片的时候 图片需要预览 ；
        let imgFile = document.querySelector(".imgFile");
        let imgshow = document.querySelector(".imgshow");
        let sure;
        imgFile.onchange = function(){
            let imgfile = this.files[0];
            if (imgfile) {
                sure = this.files[0];
                let reader = new FileReader();
                reader.readAsDataURL(imgfile);
                reader.onload = function(){
                    // console.log(this.result);  // 把图片转成base64格式
                    imgshow.innerHTML = `<img width="100%" height="100%" src="${this.result}" alt="">`;
                }
            }
        }

        // 提交上传
        let username = document.querySelector(".username");
        let btn = document.querySelector(".btn");
        let num;
        btn.onclick = async function(){

            let form = new FormData();
            form.append("advname",username.value);
            form.append("img",sure);
            let obj;
            if (num) {
                form.append("_id",num);
                obj = await axios.put("/adv/update",form);
            }else{
                obj = await axios.post("/adv/add",form);
                p = obj.data.p;
            }
            if (obj.data.status === 1) {
                mask.style.display = "none";
                adduermodel.style.display = "none";
                await getadvs(res,p);
            }    
        }

        // 删除
        let dels = document.querySelectorAll(".delBtn");
        dels.forEach(item=>{
            item.onclick = async function(){
                
            let sure = confirm("你确定要删除吗？");
            if (sure) {
                // 获取id 
                let _id = this.getAttribute("id");
                let img = this.getAttribute("img")
                // console.log(_id);
                let { data } = await axios.delete("/adv/del", {
                    params: {
                        _id,
                        img
                    }
                })
                // console.log(data);
                if (data.status === 1) {
                    if (dels.length === 1) {
                        p = p===1 ? 1 : p - 1;
                    }
                    await getadvs(res,p);
                } else {
                    console.log("删除失败");
                }
            }
            }
        })


        // 修改
        let updates = document.querySelectorAll(".update");
        updates.forEach(item=>{
            item.onclick = async function(){
                titl.innerHTML = "修改广告";
                mask.style.display = "block";
                adduermodel.style.display = "block"; 
                num = this.getAttribute("id");
            }
        })

        // 换页
        let aEles = document.querySelectorAll(".page");
        aEles.forEach(item => {
            item.onclick = async function () {
                // 获取当前的页码
                sessionStorage.setItem("p",this.innerHTML);
                p = this.innerHTML;
                await getadvs(res,p);
            }
        })
        
        // 退出登录
        let loginout = document.querySelector(".loginout");
        loginout.onclick = function(){
            localStorage.clear("token");
            window.location.href = "/login"
        }
}

export default function (router) {
    router.route("/admin/adv", (req, res) => {
        getadvs(res,p);
    })

}